<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo-placeholder"></div>
      <h2>用户登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">账号:</label>
          <input type="text" id="username" v-model="username" required>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required>
        </div>
        <div class="form-group captcha-group">
          <label for="captcha">验证码:</label>
          <input type="text" id="captcha" v-model="captcha" required>
          <img :src="captchaUrl" alt="验证码" @click="refreshCaptcha" class="captcha-image">
          <button type="button" @click="refreshCaptcha">刷新</button>
        </div>
        <button type="submit" class="login-button">登录</button>
      </form>
      <p class="register-link">
        还没有账号？ <router-link to="/register">注册</router-link>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const username = ref('');
const password = ref('');
const captcha = ref('');
const captchaUrl = ref('');
const router = useRouter();

const API_BASE_URL = 'http://localhost:9901/api';

const fetchCaptcha = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/captcha/create`);
    if (response.data && response.data.code === 200) {
      captchaUrl.value = response.data.data;
    } else {
      console.error('获取验证码失败:', response.data.message);
      // TODO: 显示错误消息给用户
    }
  } catch (error) {
    console.error('获取验证码异常:', error);
    // TODO: 显示错误消息给用户
  }
};

const refreshCaptcha = () => {
  fetchCaptcha();
};

const handleLogin = async () => {
  try {
    const response = await axios.post(`${API_BASE_URL}/user/login`, {
      username: username.value,
      password: password.value,
      captcha: captcha.value,
    });

    if (response.data && response.data.code === 200) {
      // 登录成功，保存 JWT 和用户信息，并跳转到主页
      const { jwt, username: loggedInUsername } = response.data.data;
      localStorage.setItem('jwt', jwt);
      localStorage.setItem('username', loggedInUsername);
      router.push('/'); // 跳转到主页
    } else {
      console.error('登录失败:', response.data.message);
      // TODO: 显示错误消息给用户
      alert(response.data.message); // 简单提示
      refreshCaptcha(); // 登录失败刷新验证码
    }
  } catch (error) {
    console.error('登录异常:', error);
    // TODO: 显示错误消息给用户
    alert('登录过程中发生错误。'); // 简单提示
    refreshCaptcha(); // 登录异常刷新验证码
  }
};

onMounted(() => {
  fetchCaptcha();
});
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.login-box {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 100%;
  max-width: 400px;
}

.logo-placeholder {
  width: 80px;
  height: 80px;
  background-color: #007bff; /* 占位符颜色，之后替换为随机图标 */
  margin: 0 auto 20px;
  border-radius: 50%;
}

h2 {
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #555;
}

.form-group input[type="text"],
.form-group input[type="password"] {
  width: calc(100% - 22px); /* Adjusting for padding and border */
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 1em;
}

.captcha-group {
  display: flex;
  align-items: center;
  gap: 10px; /* Space between elements */
}

.captcha-group input {
  flex-grow: 1; /* Allow input to take available space */
}

.captcha-image {
  height: 40px; /* Match input height */
  cursor: pointer;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.captcha-group button {
  padding: 10px 15px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1em;
  transition: background-color 0.3s ease;
}

.captcha-group button:hover {
  background-color: #0056b3;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1.1em;
  transition: background-color 0.3s ease;
  margin-top: 10px;
}

.login-button:hover {
  background-color: #0056b3;
}

.register-link {
  margin-top: 20px;
  font-size: 0.9em;
  color: #555;
}

.register-link a {
  color: #007bff;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .login-box {
    padding: 20px;
    margin: 20px;
  }

  .form-group input[type="text"],
  .form-group input[type="password"],
  .captcha-group input,
  .captcha-group button,
  .login-button {
    font-size: 0.9em;
    padding: 10px;
  }

  .captcha-image {
    height: 35px;
  }
}
</style> 